package org.lsy.tlais.controller;

import lombok.extern.slf4j.Slf4j;
import org.lsy.tlais.pojo.Dept;
import org.lsy.tlais.pojo.Result;
import org.lsy.tlais.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    // 查询所有部门数据
    @GetMapping
    public Result list() {
        log.info("查询全部部门数据");
        List<Dept> list = deptService.findAll();
        return Result.success(list);
    }

    // 删除部门数据
    @DeleteMapping
    public Result delete(Integer id) {
        log.info("根据id删除部门数据: {}", id);
        deptService.delete(id);
        return Result.success();
    }

    // 新增部门数据
    @PostMapping
    public Result insert(@RequestBody Dept dept) {
        log.info("新增部门数据：{}" , dept);
        deptService.insert(dept);
        return Result.success();
    }

    // 查询部门详情回显
    @GetMapping(value ="/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("根据id查询部门数据并回显：{}", id);
        Dept dept = deptService.getInfo(id);
        return Result.success(dept);
    }

    // 修改部门数据
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门数据：{}" , dept);
        deptService.update(dept);
        return Result.success();
    }
}
